﻿@using Nop.Core.Infrastructure
@using Nop.Core
@using SevenSpikes.Nop.Plugins.NopQuickTabs
@model SevenSpikes.Nop.Plugins.NopQuickTabs.Models.TabUIModel
@{
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js" type="text/javascript"></script>
   
    var cssFile = String.Format("~/Plugins/{0}/Themes/{1}/Content/jquery-ui-1.8.21.custom.css", Names.PluginFolderName, Model.Settings.Theme);
    Html.AddCssFileParts(Url.Content(cssFile));

    if (Model.Settings.EnableAjax)
    { 
    @AjaxTabs() 
    }
    else
    { 
    @NoAjaxTabs()
    }
    
    <script type="text/javascript">
        jQuery(document).ready(function ($) {
            $('#add-review').live("click", function () {
                var reviewTitle = $("#AddProductReview_Title").val();
                var reviewText = $("#AddProductReview_ReviewText").val();
                var reviewRating = $("input[id=AddProductReview_Rating]:radio:checked").val()
                var productReviewsTabAddNewUrl = "@Url.Action("ProductReviewsTabAddNew", "ProductTab", new {id = Model.ProductId})";
                $("#updateTargetId")[0].style.opacity = 0.5;
                $.ajax({
                    cache: false,
                    type: "POST",
                    url: productReviewsTabAddNewUrl,
                    data: "add-review=Submit review" + "&AddProductReview.Title=" + reviewTitle.toString()
                     + "&AddProductReview.ReviewText=" + reviewText.toString() + "&AddProductReview.Rating=" + reviewRating.toString(),
                    success: function (data) {
                        $("#updateTargetId")[0].style.opacity = 1;
                        $("#updateTargetId").html(data);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        $("#updateTargetId")[0].style.opacity = 1;
                        alert('Failed to add review.');
                    }
                });
            });
        });
    </script>
}
@helper AjaxTabs()
    {       
    <script type="text/javascript">
        jQuery(function ($) {
            $("#tabs").tabs({
                ajaxOptions: {
                    error: function (xhr, status, index, anchor) {
                        $(anchor.hash).html(
						"Couldn't load this tab.");
                    }
                }
            });
        });
    </script>    
        var storeLocation = EngineContext.Current.Resolve<IWebHelper>().GetStoreLocation();
        storeLocation = storeLocation.TrimEnd('/');

        IHtmlString defaultTabContent = new HtmlString("");
        string defaultTabTitle = String.Empty;

        IHtmlString specificationsContent = new HtmlString("");
        string specificationsTitle = String.Empty;

        bool missFirstTab = false;
        bool showSpecs = true;
        bool showReviews = true;


        if (Model.Tabs.Count == 0 &&
        !Model.Settings.EnableProductFullDescriptionTab &&
        !Model.Settings.EnableProductReviewsTab &&
        !Model.Settings.EnableProductSpecificationsTab)
        {

        }
        else
        {
    <div id="tabs" class="productTabs" style="margin-top: 10px">
        <ul>
            @if (Model.Settings.EnableProductFullDescriptionTab)
            {
                defaultTabContent = Html.Raw(@Model.Description);
                defaultTabTitle = T("SevenSpikes.NopQuickTabs.Client.Tabs.Description").ToString();
            }
            else
            {
                if (Model.Settings.EnableProductSpecificationsTab)
                {
                    defaultTabTitle = T("SevenSpikes.NopQuickTabs.Client.Tabs.Specifications").ToString();
                    defaultTabContent = Html.Action("ProductSpecifications", "ProductTab", new { id = Model.ProductId });
                    showSpecs = false;
                }
                else
                {
                    if (Model.Settings.EnableProductReviewsTab)
                    {
                        defaultTabTitle = T("SevenSpikes.NopQuickTabs.Client.Tabs.Reviews").ToString();
                        defaultTabContent = Html.Action("ProductReviewsTab", "ProductTab", new { productId = Model.ProductId });
                        showReviews = false;
                    }
                    else
                    {
                        var defaultTab = Model.Tabs.First();
                        defaultTabTitle = defaultTab.DisplayName;
                        defaultTabContent = Html.Raw(defaultTab.Description);
                        missFirstTab = true;
                    }
                }
            }
            <li id="firstTabTitle"><a href="#tabs-1">@defaultTabTitle</a> </li>
            @if (Model.Settings.EnableProductSpecificationsTab && showSpecs)
            { 
                <li><a href="@storeLocation/ProductTab/ProductSpecificationsTab/@Model.ProductId">@T("SevenSpikes.NopQuickTabs.Client.Tabs.Specifications").ToHtmlString()</a>
                </li>
            }
            @if (Model.Settings.EnableProductReviewsTab && showReviews)
            { 
                <li><a href="@storeLocation/ProductTab/ProductReviewsTab/@Model.ProductId">@T("SevenSpikes.NopQuickTabs.Client.Tabs.Reviews").ToHtmlString()</a>
                </li>
            }
            @foreach (var tab in Model.Tabs)
            {
                if (!missFirstTab)
                {
                <li><a href="@storeLocation/ProductTab/ProductTab/@tab.Id">@tab.DisplayName</a>
                </li>
                }
                else
                {
                    missFirstTab = false;
                }
            }
        </ul>
        <div class="clear">
        </div>
        <div id="tabs-1">
            @defaultTabContent
        </div>
    </div>
        }
}
@helper NoAjaxTabs()
    {
    <script type="text/javascript">
        jQuery(function ($) {
            $("#tabs").tabs();
        });
    </script>
    
        IHtmlString defaultTabContent = new HtmlString("");
        string defaultTabTitle = String.Empty;

        IHtmlString specificationsContent = new HtmlString("");
        string specificationsTitle = String.Empty;

        bool missFirstTab = false;
        bool showSpecs = true;
        bool showReviews = true;

        if (Model.Tabs.Count == 0 &&
        !Model.Settings.EnableProductFullDescriptionTab &&
        !Model.Settings.EnableProductReviewsTab &&
        !Model.Settings.EnableProductSpecificationsTab)
        {

        }
        else
        {
            List<KeyValuePair<string, string>> divs = new List<KeyValuePair<string, string>>();
        
    <div id="tabs" class="productTabs" style="margin-top: 10px">
        <ul>
            @if (Model.Settings.EnableProductFullDescriptionTab)
            {
                divs.Add(new KeyValuePair<string, string>("tabs-description", Model.Description));
                <li><a href="#tabs-description">@T("SevenSpikes.NopQuickTabs.Client.Tabs.Description").ToHtmlString()</a>
                </li>                
            }
            @if (Model.Settings.EnableProductSpecificationsTab)
            {
                var specificaions = Html.Action("ProductSpecificationsTab", "ProductTab", new { id = Model.ProductId });
                divs.Add(new KeyValuePair<string, string>("tabs-specs", specificaions.ToString()));
                <li><a href="#tabs-specs">@T("SevenSpikes.NopQuickTabs.Client.Tabs.Specifications").ToHtmlString()</a>
                </li>  
            }
            @if (Model.Settings.EnableProductReviewsTab)
            {
                var reviews = Html.Action("ProductReviewsTab", "ProductTab", new { id = Model.ProductId });
                divs.Add(new KeyValuePair<string, string>("tabs-reviews", reviews.ToString()));
                <li><a href="#tabs-reviews">@T("SevenSpikes.NopQuickTabs.Client.Tabs.Reviews").ToHtmlString()</a>
                </li>                   
            }
            @foreach (var tab in Model.Tabs)
            {
                divs.Add(new KeyValuePair<string, string>("tabs-" + tab.Id.ToString(), tab.Description));
                <li><a href="#tabs-@tab.Id">@tab.DisplayName</a> </li>                
            }
            @* Uncomment the code bewlow if you want to have a Tab that gets its information from a Topic. This tab will be shown for all products in your store*@
            @*<li><a href="#tab-ShippingInfo">@T("ShippingReturns")</a> </li>   *@
        </ul>
        @foreach (var pair in divs)
        {
            <div id="@pair.Key">
                @Html.Raw(@pair.Value)
            </div>
        }
        @* Uncomment the code bewlow if you want to have a Tab that gets its information from a Topic. This tab will be shown for all products in your store *@
        @*<div id="tab-ShippingInfo">
        @Html.Action("TopicBlock", "ProductTab", new { systemName = "ShippingInfo" })
        </div>*@
    </div>
        }
}
